package com.easy.service.impl;

import com.easy.bean.UserBean;
import com.easy.mapper.UserDao;
import com.easy.util.LogMethod;
import com.easy.util.Page;
import com.easy.service.UserService;
import com.easy.util.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.easy.util.PasswordUtil.getEncryption;

@Service
//@Transactional
public class  UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Override
    @Transactional
    @LogMethod(serviceName = "用户添加",recordReturn = true)
    public UserBean add(UserBean user) {
        user.setIsdel(1); // 设置默认状态为启用
        user.setRole("user"); // 设置默认角色为普通用户

        // 修改：对密码进行加密处理
        if (user.getUserPassword() != null && !user.getUserPassword().isEmpty()) {
            String encryptedPassword = PasswordUtil.getEncryption(user.getUserPassword());
            user.setUserPassword(encryptedPassword);
        }

        userDao.add(user);
        return userDao.get(user.getId());
    }

    @Override
    @Transactional
    @LogMethod(serviceName = "用户注销",recordReturn = true)
    public int del(Integer id) {
        return userDao.del(id);
    }

    @Override
    @Transactional
    @LogMethod(serviceName = "用户删除",recordReturn = true)
    public int delete(Integer id) {
        return userDao.delete(id);
    }


//    @Override
//    public UserBean edit(UserBean user) {
//        userDao.edit(user);
//        return userDao.get(user.getId());
//    }

    @Override
    @Transactional
    @LogMethod(serviceName = "用户管理",recordReturn = true)
    public UserBean edit(UserBean user) {
        // 修改：如果修改了密码，也需要加密
        if (user.getUserPassword() != null && !user.getUserPassword().isEmpty()) {
            // 检查密码是否已经是加密格式（避免重复加密）
            if (!user.getUserPassword().contains("$")) { // 简单判断，您可以根据加密后的特征调整

                String encryptedPassword = PasswordUtil.getEncryption(user.getUserPassword());
                user.setUserPassword(encryptedPassword);
            }
        }
        userDao.edit(user);
        return userDao.get(user.getId());
    }

    @Override
    public List<UserBean> list(UserBean user) {
        return userDao.list(user);
    }


    @Override
    public UserBean get(Integer id) {
        return userDao.get(id);
    }

    @Override
    public void page(Page page, UserBean user) {
        int count = userDao.pageCount(user);
        List<UserBean> data = userDao.page(page, user);
        page.setCount(count);
        page.setData(data);
    }
    @Override
    @LogMethod(serviceName = "当前用户",recordReturn = true)
    public UserBean getUserInfo(Integer id) {
        return userDao.get(id);
    }

    @Override
    @LogMethod(serviceName = "验证密码",recordReturn = true)
    public boolean validatePassword(Integer id, String inputPassword) {
        UserBean user = userDao.validatePassword(id);
        if (user == null) {
            return false;
        }
        // 使用 PasswordUtil 验证密码
        return PasswordUtil.checkPassword(inputPassword, user.getUserPassword());
    }
    @Override
    public UserBean getByNumber(String userNumber) {
        return userDao.getByNumber(userNumber);
    }


}

